package com.example.springboot.config;

import com.example.springboot.filter.TokenAuthFilter;
import com.example.springboot.filter.TokenLoginFilter;
import com.example.springboot.security.DefaultPasswordEncoder;
import com.example.springboot.security.TokenLogoutHandler;
import com.example.springboot.security.TokenManager;
import com.example.springboot.security.UnauthEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {


    private RedisTemplate<String,String> redisTemplate;
    private UserDetailsService userDetailsService;

    @Autowired
    public TokenWebSecurityConfig(RedisTemplate<String, String> redisTemplate, UserDetailsService userDetailsService) {
        this.redisTemplate = redisTemplate;
        this.userDetailsService = userDetailsService;
    }

    /**
     * 配置过滤器
     * @default
    **/
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            // 设置登录信息，如登录URL
            .formLogin()
            .loginProcessingUrl("/login")
            // 异常处理
            .and().exceptionHandling()
            //没有权限访问
            .authenticationEntryPoint(new UnauthEntryPoint())
            // 关闭csrf
            .and().csrf().disable()
            // 权限配置
            .authorizeRequests()
            // 不拦截登录路径
            .antMatchers("/login","/imageCode","/db/select").permitAll()
            .anyRequest().authenticated()
            // 设置退出路径
            .and().logout().logoutUrl("/logout")
            .addLogoutHandler(new TokenLogoutHandler(tokenManager(),redisTemplate)).and()
            .addFilter(new TokenLoginFilter(authenticationManager(),tokenManager(),redisTemplate))
            .addFilter(new TokenAuthFilter(authenticationManager(),tokenManager(),redisTemplate))
            .httpBasic();
    }
    /**
     * 调用 userDetailsService 和密码处理
     * @default
    **/
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    /**
     * 不授权可访问的路径
     * @default
    **/
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/api/**");
    }

    /**
     * 密码
     * @default
    **/
    @Bean
    PasswordEncoder passwordEncoder(){
        return new DefaultPasswordEncoder();
    }

    @Bean
    TokenManager tokenManager(){
        return new TokenManager();
    }

}
